var couponsCom = {
    template: '#coupon-box',
    props: ['coupons', 'show'],
    data: function () {
        return {}
    },
    watch: {},
    computed: {},
    methods: {
        hide_box: function () {
            this.$emit('hide_box');
        },
        show_amount: function (coupon_type, amount) {
            return coupon_type ? '<em>' + amount + '</em>折' : '￥<em>' + amount + '</em>';
        },
        show_expire: function (coupon) {
            if (!coupon) return '已过期';
            var txt = '有效期 ';
            switch (coupon.expire_type) {
                case 0:
                    txt += date('yyyy-MM-dd', coupon.start_at) + '~' + date('yyyy-MM-dd', coupon.end_at);
                    break;
                case 1:
                    txt += '领到券当日开始' + coupon.expire_days + '天内生效';
                    break;
                case 2:
                    txt += '领到券当次日开始' + coupon.expire_days + '天内生效';
                    break;
            }
            return txt;
        },
        get_coupon: function (id) {
            var ele = event.target;
            if (!id) return;
            if ($(ele).hasClass('has')) return;
            var coupon = null;
            var index = null;
            for (var i in this.coupons) {
                if (this.coupons[i].id == id) {
                    coupon = this.coupons[i];
                    index = i;
                }
            }
            if (coupon) {
                $.ajax({
                    url: "/wap/coupon/receive/" + coupon.id,
                    type: 'post',
                    dataType: 'json',
                    success: function (result) {
                        tips(result.message);
                        $(ele).addClass('has');
                        ele.innerHTML = result.message;
                    }
                });
            } else {
                tips('优惠券不存在');
            }
        }
    }
};

Vue.component('coupon-box', couponsCom);